//
//  SettingViewController.m
//  XPhoto
//
//  Created by baboy on 2020/4/19.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "SettingViewController.h"
#import "MediaEditTableViewCell.h"
#import "ButtonTableViewCell.h"
#import "SysDict.h"
#import "User.h"
#import "BKWebViewController.h"
#import <BKKit/BKNavigationController.h>
#import "PasswordResetViewController.h"

//gray5color
#define LOGOUT_BUTTON_BG_COLOR      [UIColor colorWithRed:0.8980392157 green:0.8980392157 blue:0.91764705879999997 alpha:1]
//gray4color
#define LOGOUT_BUTTON_BORDER_COLOR  [UIColor colorWithRed:0.81960784310000001 green:0.81960784310000001 blue:0.83921568629999999 alpha:1]

@interface SettingViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) NSArray *rows;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = self.headerView;
    self.rows = @[
        @{
            @"title":NSLocalizedString(@"alter_password", @"标签"),
            @"key":@"alter_password"
        },
        @{
            @"title":NSLocalizedString(@"about", @"标签"),
            @"link":@"about_link"
        },
        @{
            @"title":NSLocalizedString(@"feedback", @"标签"),
            @"link":@"feedback_link"
        },
        @{
            @"title":NSLocalizedString(@"version", @"标签"),
            @"link":SYS_DICT_ITEM_KEY_APP_UPGRADE_LINK
        },
        @{
            @"key":@"logout"
        }
    ];
}
#pragma mark -UITableViewCell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.rows.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *conf = [self.rows objectAtIndex:indexPath.row];
    if ([@"logout" isEqualToString:conf[@"key"]]) {
        ButtonTableViewCell *cell = loadViewFromNib([ButtonTableViewCell class], nil);
        [cell.button addTarget:self action:@selector(logout:) forControlEvents:UIControlEventTouchUpInside];
        [cell.button setTitle:NSLocalizedString(@"logout", @"标签") forState:UIControlStateNormal];
        [cell.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        cell.button.backgroundColor = LOGOUT_BUTTON_BG_COLOR;
        cell.button.layer.borderColor = LOGOUT_BUTTON_BORDER_COLOR.CGColor;
        cell.button.layer.borderWidth = 0.5;
        NSLayoutConstraint *topConstraint = [cell.button.superview constraintForAttribute:NSLayoutAttributeTop firstItem:cell.button secondItem:cell.button.superview];
        topConstraint.constant = 20;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    MediaEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MediaEditTableViewCell class])];
    if (!cell) {
        cell = loadViewFromNib([MediaEditTableViewCell class], nil);
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.titleLabel.text = conf[@"title"];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *conf = [self.rows objectAtIndex:indexPath.row];
    if ([@"logout" isEqualToString:conf[@"key"]]) {
        return;
    }
    //修改密码
    if([@"alter_password" isEqualToString:conf[@"key"]]){
        PasswordResetViewController *vc = [[PasswordResetViewController alloc] init];
        BKNavigationController *nav = [[BKNavigationController alloc] initWithRootViewController:vc];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:nav animated:YES completion:^{
            
        }];
    }
    
    NSString *link = conf[@"link"];
    //升级
    if ([SYS_DICT_ITEM_KEY_APP_UPGRADE_LINK isEqualToString:link]) {
        SysDictItem *linkItem = [SysDict itemForKey:link dictKey:SYS_DICT_KEY_WEB_LINK];
        if (linkItem) {
            [APP openURL:[NSURL URLWithString:linkItem.value] options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:nil];
        }
        return;
    }
    if (link) {
        SysDictItem *linkItem = [SysDict itemForKey:link dictKey:SYS_DICT_KEY_WEB_LINK];
        if (linkItem) {
            BKWebViewController *vc = [[BKWebViewController alloc] init];
            vc.currentURL = [NSURL URLWithString:linkItem.value];
            BKNavigationController *nav = [[BKNavigationController alloc] initWithRootViewController:vc];
            nav.modalPresentationStyle = UIModalPresentationFullScreen;
            [self presentViewController:nav animated:YES completion:^{
                
            }];
            
        }
        return;
    }
}
- (void)logout:(id)sender{
    [User logout];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
